<?php
/**
 * Created by IntelliJ IDEA.
 * User: admin
 * Date: 2020/12/23
 * Time: 13:38
 */

namespace app\api\controller;


use app\common\module\Wechat;
use think\Db;

class Wx
{



    public function msg()
    {
        $xml = file_get_contents('php://input');
        $array = xmlToArray($xml);
        $open_id = $array['FromUserName'];
        $wechat = new Wechat();
        $userInfo = $wechat->PublicUnionId($open_id);
        if(isset($userInfo['unionid']) && $userInfo['unionid']){
            Db::name('users')
                ->where('unionid',$userInfo['unionid'])
                ->update([
                    'public_openid'=>$userInfo['openid'],
                    'nickname'=>$userInfo['nickname'],
                    'headimgurl'=>$userInfo['headimgurl'],
                ]);
            Db::name('follow')->insert([
                'openid'=>$userInfo['openid'],
                'unionid'=>$userInfo['unionid'],
                'nickname'=>$userInfo['nickname'],
                'subscribe'=>$userInfo['subscribe'],
                'headimgurl'=>$userInfo['headimgurl'],
                'msg'=>json_encode($userInfo),
                'create_time'=>date("Y-m-d H:i:s"),
                'qr_scene_str'=>$userInfo['qr_scene_str']??""
            ],true);
        }
        $toUserMsg = [
            'ToUserName' => $array['FromUserName'],
            'FromUserName' => $array['ToUserName'],
            'CreateTime' => time(),
            'MsgType' => 'text',
            'Content' => '您好，感谢您的关注
    废为宝回收是一家运营“互联网+废品回收”的生活类服务平台企业，专注于各类废品回收[爱心]
    公司通过互联网方式，运用微信公众号、小程序，致力于为企业级客户、商户（商场、超市、写字楼、学校、医院、快递等）、社区提供更正规、快速、高效的再生资源上门回收服务[拳头]
    <br/>
招募合伙人：18982680009'
        ];

        if(isset($array['Event']) && $array['Event']=='subscribe') { //关注事件
            $toUserMsg = [
                'ToUserName' => $array['FromUserName'],
                'FromUserName' => $array['ToUserName'],
                'CreateTime' => time(),
                'MsgType' => 'text',
                'Content' => '您好，感谢您的关注
    废为宝回收是一家运营“互联网+废品回收”的生活类服务平台企业，专注于各类废品回收[爱心]
    公司通过互联网方式，运用微信公众号、小程序，致力于为企业级客户、商户（商场、超市、写字楼、学校、医院、快递等）、社区提供更正规、快速、高效的再生资源上门回收服务[拳头]
招募合伙人：18982680009'
            ];
            echo arrayToXml($toUserMsg);
        }else if(isset($array['Event']) && $array['Event'] =='unsubscribe'){ //取消关注事件
//            $shareModel->deleteWechatQr($array['FromUserName']);
        }
        echo arrayToXml($toUserMsg);
    }
    public function createMenu(){
        exit;
        $we  = new Wechat();
        $acc = $we->getPublicAccess();
        $url = 'https://api.weixin.qq.com/cgi-bin/menu/create?access_token='.$acc;
        $data  ='{"button":[{"type":"miniprogram","name":"一键下单","url":"http://mp.weixin.qq.com","appid":"wx5f5d404d20793e35","pagepath":"pages/index/index"},{"type":"miniprogram","name":"我的订单","url":"http://mp.weixin.qq.com","appid":"wx5f5d404d20793e35","pagepath":"pages/order/index"},{"name":"菜单","sub_button":[{"type":"miniprogram","name":"主页","url":"http://mp.weixin.qq.com","appid":"wx5f5d404d20793e35","pagepath":"pages/index/index"},{"type":"miniprogram","name":"回收员","url":"http://mp.weixin.qq.com","appid":"wx5f5d404d20793e35","pagepath":"pages/user/recruit/index"},{"type":"miniprogram","name":"回收员申请","url":"http://mp.weixin.qq.com","appid":"wx5f5d404d20793e35","pagepath":"pages/user/recruit/index"}]}]}';
        $result = curl_post($url,$data);
        print_v($result);
    }



}